/* ====================================== toast  ====================================== */
import React from 'react'
// ===================================================================== Temp
import Table from '#cpt/temp/Table'
// =====================================================================
const { $Lazy, $fn } = window
// ===================================================================== Component
const Select 	= $Lazy.load(()=>import('@antd/form/select'))
//
// const $cache = $Lazy.file(()=>import('#private/cache-api'))
// ===================================================================== 
class Index extends React.Component {
	state = {
		pag:{ },
		selectedKeys:[],
	}
	
	close = () => this.setState({ open:false })
	
	componentDidMount(){
		window.addEventListener('click', this.close)
	}
	componentWillUnmount(){
		window.removeEventListener('click', this.close)
	}
	
	onChange = (value, rows) => {
		let { name, onChanged, onChange, outer, type=1 } = this.props
		value = $fn.isValid(value) ? value : []
		
		if(!outer){ 
			this.setState({ value, open:false }) }
		else{
			this.close()
		}
		
		let model = name ? { [name]: value } : value
		if( type === 1){
			value = value[0]
			rows = rows[0]
		}
		
		onChanged?.({ model, value, rows, name })
		onChange?.(value)
		
	}
	
	
	render(){
		let { open, data } = this.state
		let  { idStr='value', nameStr='name', type=1, cols = [] } = this.props
		data = data ? data :  this.props.data
		return (
			<Select 
				dropdownRender			= { Table.call(this, { cols, data, type, wrapClassName:'mlr5 kindSelect', height:300, idStr, size:'small', onSelectOk: this.onChange }) }
				{...this.props}
				data					= { data }
				idStr 					= { idStr }
				nameStr					= { nameStr }
				open 					= { open }
				onClick 				= { ()=> {
					this.setState({open:true}, ()=>{
						setTimeout(()=> this.setState({ data:null }) )
					})
				} }
				onSearch				= { value => {
					// 搜索不区分大小写 20220705 耿琰
					const stack = $fn.copy(this.props.data).filter( v => {
						return cols.some( p => {
							return	v[p.dataIndex].indexOf( value ) !== -1 ||  
									v[p.dataIndex].indexOf( value.toLowerCase() ) !== -1 ||
									v[p.dataIndex].indexOf( value.toUpperCase() ) !== -1
						})
					})
					this.setState({ data: stack })
				}}
			/>
		)
	}
}
export default Index